package view.type;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import service.book.BookTypeServer;
import service.book.BookTypeServerImpl;
import utils.ComonUtil;
import view.CommonView.CommonJPanel;
import view.user.AddUserView;

import entity.BookType;


/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 类别添加界面模块
 *
 */
public class AddTypeView extends CommonJPanel {

	// 创建输入框
	JTextField typeNameField;
	JTextArea typeDescField;
	
	private BookTypeServer bookTypeServer = new BookTypeServerImpl();

	public AddTypeView() {
		super(TYPE_TRANSPARENT);
		setLayout(new FlowLayout(FlowLayout.CENTER, 100, 50));
		// 图书类别标签、输入框面板
		JPanel nameFieldPanel = new CommonJPanel(TYPE_TRANSPARENT);
		nameFieldPanel.setLayout(new GridLayout(1, 2));
		add(nameFieldPanel);

		// 创建图书类别名称标签
		JLabel labelTypeName = new JLabel("图书类别名称：");
		labelTypeName.setFont(new Font("微软雅黑", Font.BOLD, 20));
		nameFieldPanel.add(labelTypeName);
		// 创建图书类别名称输入框
		typeNameField = new JTextField();
		typeNameField.setPreferredSize(new Dimension(300, 30));
		typeNameField.setFont(new Font("微软雅黑", Font.BOLD, 20));
		nameFieldPanel.add(typeNameField);

		// 图书类别标签、输入框面板
		JPanel descFieldPanel =new CommonJPanel(TYPE_TRANSPARENT);
		descFieldPanel.setLayout(new GridLayout(1, 2));
		add(descFieldPanel);

		// 创建图书类别描述标签
		JLabel labelTypeDesc = new JLabel("图书类别描述：");
		labelTypeDesc.setFont(new Font("微软雅黑", Font.BOLD, 20));
		descFieldPanel.add(labelTypeDesc);
		// 创建图书类别描述输入框
		typeDescField = new JTextArea();
		// 自动换行
		typeDescField.setLineWrap(true);
		typeDescField.setWrapStyleWord(true);

		typeDescField.setPreferredSize(new Dimension(300, 150));
		typeDescField.setFont(new Font("微软雅黑", Font.BOLD, 20));
		typeDescField.setBackground(new Color(246, 246, 246));
		descFieldPanel.add(typeDescField);

		// 按钮面板
		JPanel buttonPanel = new CommonJPanel(TYPE_TRANSPARENT);
		buttonPanel.setLayout(new GridLayout(1, 2, 80, 0));
		buttonPanel.setFont(new Font("微软雅黑", Font.BOLD, 20));
		add(buttonPanel);

		// 创建添加按钮
		JButton add = new JButton("添加");
		buttonPanel.add(add);

		// 创建重置按钮
		JButton reset = new JButton("重置");
		buttonPanel.add(reset);

		// 添加监听器
		// 添加按钮
		add.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (ComonUtil.isEmpty(typeNameField.getText()) || ComonUtil.isEmpty(typeDescField.getText())) {
					JOptionPane.showMessageDialog(AddTypeView.this, "图书类型名称和描述不能为空！");
					return;
				}
				BookType bookType = new BookType();
				bookType.setBookTypeName(typeNameField.getText());
				bookType.setBookTypeDesc(typeDescField.getText());
				

				
				String flag = bookTypeServer.addBookType(bookType)?"添加成功！":"添加失败";
				JOptionPane.showMessageDialog(AddTypeView.this, flag);
      
				

			}
		});

		// 重置按钮
		reset.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				typeNameField.setText("");
				typeDescField.setText("");
			}
		});

	}

	/**
	 * 重置输入框
	 */
	public void resetInput() {
		typeNameField.setText("");
		typeDescField.setText("");
	}

}
